// sddamd.cpp
// single dimension dynamic array mapped as multi dimensional

#include "auxlib.h"
#include <iostream>
using namespace std;
#include "sddamd.h"

#define A(p,i,j,r) (p[(i)*(r)+(j)])

void fillMatrixSddamd(double *p, int r, int c, double d) 
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			A(p,i,j,r) = d; // p[i][j] = d;
		}
	}
}

void addToMatrixSddamd(double *p, int r, int c, double d) 
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			A(p,i,j,r) += d; // p[i][j] += d;
		}
	}
}

void single_dimension_dynamic_array_mapped_as_multi_dimensional(int size) 
{
	cout << "=== single_dimension_dynamic_array_mapped_as_multi_dimensional, size=" 
		<< size << endl;
	double eps = 1e-8;
	MyClock clk;
	double duration_sec;
	clk.markTime();
	// code to test begin (1)
	int row = size, col = size;
	double *p = new double [row * col];
	fillMatrixSddamd(p,row,col,1.1);
	duration_sec = clk.getDiffTime();
	cout << "alloc & fill matrix=" << duration_sec << " sec" << endl;
	// code to test begin (2)
	clk.markTime();
	addToMatrixSddamd(p,row,col,2.2);
	duration_sec = clk.getDiffTime();
	cout << "addToMatrix=" << duration_sec << " sec" << endl;
	// code to test end (2)

	// confirm that add was done
	myAssert(nearEqual(A(p,2,3,row),3.3,eps));

	// code to test begin (3)
	clk.markTime();
	delete [] p;
	duration_sec = clk.getDiffTime();
	cout << "deleteMatrix=" << duration_sec << " sec" << endl;
	// code to test end (3)
}
